<?php

//id
//owner
//org_id
//create_time
//update_time
//name
//token
//alias
//pid
//gid
//description
//status
//ota_version
//ext
//parent_id
//device_type
//link_status
//last_time
require_once './devices_data.php';
$length = count($device_data);
$pids_length = count($pids);

//var_dump($device_data,$length,$pids_length);
//die;
for ($i = 0; $i < 20000; $i++) {
    $randNum = rand(0,108);
    $data[] = [
        "id" => generateRandomCode(),
        'owner' => 'doumingquan',
        "org_id" => 2,
        "create_time" => date('Y-m-d H:i:s',strtotime('now')),
        "update_time" => date('Y-m-d H:i:s',strtotime('now')),
        "name" => $device_data[$randNum][0], //型号简称
        "token" => generateRandomCode(48),
        "alias" => $device_data[$randNum][0], //同型号简称
        "pid" => $pids[rand(0,259)], //产品id
        "gid" => '9uOQ1Ku0PQ', //分组id
        "description" => $device_data[$randNum][1],
        "status" => 0,
        "ota_version" => '',
        "ext" => null,
        "parent_id" => '' ,
        "device_type" => 'gateway',
        "link_status" => 'offline',
        "last_time" => date('Y-m-d H:i:s'),
    ];
}
// var_dump($data);die;
$host = '192.168.50.67';
$dbname = 'dim5_iot';
$userName = 'root';
$password = '123456';
$pdo = new PDO('mysql:host=' . $host . ';dbname=' . $dbname, $userName, $password, [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_EMULATE_PREPARES => false,
    // 其他 PDO 属性...
]);
try {
    // PDO 连接数据库
    // $pdo = new PDO('mysql:host=localhost;dbname=test', 'root', 'root', [
    //     PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    //     PDO::ATTR_EMULATE_PREPARES => false,
    //     // 其他 PDO 属性...
    // ]);

    // 准备 SQL 语句（对于单条插入）
    $sql = "INSERT INTO devices (id,owner,org_id,create_time,update_time,name,token,alias,pid,gid,description,status,ota_version,ext,parent_id,device_type,link_status,last_time) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    // 使用事务确保批量操作的一致性
    $pdo->beginTransaction();

    // 准备预处理语句
    $stmt = $pdo->prepare($sql);

    // 批量绑定并执行
    $chunkSize = 1000; // 你可以根据需要调整这个值
    $chunkCount = ceil(count($data) / $chunkSize);

    for ($i = 0; $i < $chunkCount; $i++) {
        $offset = $i * $chunkSize;
        $chunk = array_slice($data, $offset, $chunkSize);

        foreach ($chunk as $row) {
            $stmt->execute([$row["id"], $row["owner"], $row["org_id"], $row["create_time"], $row["update_time"], $row["name"], $row["token"], $row["alias"], $row["pid"], $row["gid"], $row["description"], $row["status"], $row["ota_version"], $row["ext"], $row["parent_id"], $row["device_type"], $row["link_status"], $row["last_time"]]);
        }
    }

    // 提交事务
    $pdo->commit();
    echo "批量插入完成！";
} catch (PDOException $e) {
    // 如果发生异常，回滚事务
    $pdo->rollBack();

    // 输出错误信息
    echo "批量插入失败: " . $e->getMessage();
}

// 关闭 PDO 连接（尽管 PHP 会在脚本结束时自动关闭，但显式关闭是一个好习惯）
$pdo = null;

function generateRandomCode($length = 12)
{
    $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $code .= 'abcdefghijklmnopqrstuvwxyz';
    $code .= '0123456789';

    // 将字符串打乱，创建一个包含所有可能字符的数组
    $shuffledCode = str_shuffle($code);

    // 从打乱后的字符串中取出前$length位字符作为随机码
    $randomCode = substr($shuffledCode, 0, $length);

    return $randomCode;
}
//